package j2metris.util;

/**
 * The normal modulo behavior is to count backwards from zero if the
 * left-hand operand is negative. This class will perform exactly the same
 * as the regular modulo function, except it will count backwards from the
 * <i>right-hand operand</i> if the left-hand operand is negative.
 */
public class LoopingModulo
{
    private LoopingModulo() {} //to prevent instantiation
    /**
     * Calculates the "looping mod" of two numbers.
     *
     * @param left the left-hand operand
     * @param right the right-hand operand
     * @return <code>left</code> mod <code>right</code>,
     * with the qualification described above
     */
    public static int calculate(int left, int right)
    {
        if(right == 1 || right == 0) return 0;
        if(left < 0) return right + (left % right);
        return left % right;
    }
}
